﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class BaseData_ProductColorEdit : System.Web.UI.Page
{
    protected void Page_PreInit(object sender, EventArgs e)
    {
        Page.Theme = BLL.Common.GetWSMThemes();
    }

    private string Code
    {
        get
        {
            if (Request.QueryString["code"] != null)
            {
                return Request.QueryString["code"].ToString();
            }
            else
            {
                return "";
            }
        }
    }

    private Model.GUIGE1 ProductColor
    {
        get
        {
            Model.GUIGE1 color = null;
            if (ViewState["ProductColor"] != null)
            {
                color = (Model.GUIGE1)ViewState["ProductColor"];
            }
            return color;
        }
        set
        {
            ViewState["ProductColor"] = value;
        }
    }


    private bool IsAdd
    {
        get
        {
            return Convert.ToBoolean(ViewState["IsAdd"]);
        }
        set
        {
            ViewState["IsAdd"] = value;
        }

    }


    protected void Page_Load(object sender, EventArgs e)
    {
        Check.CheckPermission("200200101");
        if (!IsPostBack)
        {
            if (this.Code != "")
            {
                this.ProductColor = new BLL.GUIGE1().GetModel(this.Code);
                if (this.ProductColor != null)
                {
                    txtCode.Text = this.ProductColor.GGDM;
                    txtName.Text = this.ProductColor.GGMC;
                    txtColor.Text = this.ProductColor.COLOR;
                    txtRGB.Text = this.ProductColor.RGB;
                    txtCMYK.Text = this.ProductColor.CMYK;
                    TextBox1.Text = this.ProductColor.BYZD1;
                    txtCode.Enabled = false;
                    this.IsAdd = false;
                    btnAddOrUpdate.Text = "更新";
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('系统中无此记录!');location.href='ProductColor.aspx';</script>");
                }
            }
            else
            {
                this.IsAdd = true;
                btnAddOrUpdate.Text = "添加";
            }
            btnCancel.Attributes.Add("onclick", "if(!confirm('要取消" + btnAddOrUpdate.Text + "并返回上一页吗?'))return false;");
        }
    }

    protected void btnAddOrUpdate_Click(object sender, EventArgs e)
    {
        if (this.IsAdd == true)
        {
            if (Check.CheckPermission("200200101", "00001"))
            {
                if (txtCode.Text != "")
                {
                    Model.GUIGE1 color = new BLL.GUIGE1().GetModel(txtCode.Text.Trim());
                    if (color == null)
                    {
                        color = new Model.GUIGE1();
                        color.GGDM = txtCode.Text;
                        color.GGMC = txtName.Text;
                        color.COLOR = txtColor.Text;
                        color.RGB = txtRGB.Text;
                        color.CMYK = txtCMYK.Text;
                        color.BYZD1 = TextBox1.Text;
                        if (new BLL.GUIGE1().Add(color) == color.GGDM)
                        {
                            Share.InsertLog("200200101", "00001");
                            Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('添加记录成功');location.href='ProductColor.aspx';</script>");
                        }
                        else
                        {
                            Share.Alert(this.Page, "添加记录失败,请重新添加。");
                        }
                    }
                    else
                    {
                        Share.Alert(this.Page, "已经存在代码为" + txtCode.Text.Trim() + "的记录!");
                    }
                }
                else
                {
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('请输入代码。');</script>");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='ProductColor.aspx';</script>");
            }
        }
        else
        {
            if (Check.CheckPermission("200200101", "00002"))
            {
                this.ProductColor.GGMC = txtName.Text;
                this.ProductColor.COLOR = txtColor.Text;
                this.ProductColor.RGB = txtRGB.Text;
                this.ProductColor.CMYK = txtCMYK.Text;
                this.ProductColor.BYZD1 = TextBox1.Text;
                if (new BLL.GUIGE1().Update(this.ProductColor))
                {
                    Share.InsertLog("200200101", "00002");
                    Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('更新记录成功');location.href='ProductColor.aspx';</script>");
                }
                else
                {
                    Share.Alert(this.Page,"更新记录失败,请重新编辑更新。");
                }
            }
            else
            {
                Page.RegisterStartupScript("StartUp", "<script language=javascript>alert('无权进行此操作');location.href='ProductColor.aspx';</script>");
            }

        }
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        Response.Redirect("ProductColor.aspx");
    }
}
